package cn.msi.question.service;

import java.util.List;

import cn.common.manager.SessionManager;
import cn.common.util.GeneralUtils;
import cn.msi.customer.domain.User;
import cn.msi.question.domain.Sort;
import cn.msi.question.mapper.SortMapper;

/**
 * 样题库分类业务处理类
 * 
 * @author Administrator
 * 
 */
public class SortService {

	private SortMapper sortMapper;

	/**
	 * 搜索样题分类
	 * 
	 * @param sort
	 * @return
	 */
	public String searchSort(Sort sort) {
		User user = (User) SessionManager.getAttr("user");
		sort.setCustId(user.getCustId());
		List<Sort> sortList = sortMapper.selectList(sort);
		for (Sort s : sortList) {
			s.setId(s.getSortId());
			s.setText(s.getSortName());
			addSortNode(s);
		}
		return GeneralUtils.easyUI(sortList);
	}

	/**
	 * 保存样题分类
	 * 
	 * @param sort
	 * @return
	 */
	public String addSort(Sort sort) {
		User user = (User) SessionManager.getAttr("user");
		sort.setCustId(user.getCustId());
		sortMapper.insert(sort);
		sort.setId(sort.getSortId());
		sort.setText(sort.getSortName());
		return GeneralUtils.easyUI(sort);
	}

	/**
	 * 删除分类信息
	 * 
	 * @param sort
	 */
	public void delSort(Sort sort) {
		sortMapper.deleteById(sort.getSortId());
	}

	/**
	 * 增加节点
	 * 
	 * @param sort
	 */
	private void addSortNode(Sort sort) {
		List<Sort> sortList = sortMapper.selectByParentId(sort.getSortId());
		for (Sort s : sortList) {
			s.setId(s.getSortId());
			s.setText(s.getSortName());
			addSortNode(s);
			sort.getChildren().add(s);
		}
	}

	public void setSortMapper(SortMapper sortMapper) {
		this.sortMapper = sortMapper;
	}
}
